package com.wisewin.circle.api.controller;

import com.wisewin.circle.api.controller.base.BaseController;
import com.wisewin.circle.config.dto.ResultDTO;
import com.wisewin.circle.config.dto.ResultDTOBuilder;
import com.wisewin.circle.entity.LoginUser;
import com.wisewin.circle.entity.bo.QueryParamBO;
import com.wisewin.circle.entity.db.*;
import com.wisewin.circle.service.ActivityService;
import com.wisewin.circle.service.TrendHeartService;
import com.wisewin.circle.service.TrendService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/trend")
public class TrendController extends BaseController {
    final static Logger log = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private TrendService trendService;
    @Autowired
    private ActivityService activityService;
    @Autowired
    private TrendHeartService trendHeartService;

    // 上传动态文件
    @PostMapping("/uploadTrendUrl")
    public ResultDTO uploadTrendUrl(HttpServletRequest request, MultipartFile[] lstFile) {
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            return ResultDTOBuilder.failure("0000005", "必传参数为空", null);
        }
        // 获取登录用户
        LoginUser loginUser = super.getLoginUser(token);
        if (loginUser == null ) { // 判断是否登录
            return ResultDTOBuilder.failure("0000001", "登录已过期", null);
        }
        return activityService.uploadFile(lstFile, loginUser.getId(), "trendUrl");
    }

    // 添加动态
    @PostMapping("/addTrend")
    public ResultDTO addTrend (HttpServletRequest request, @RequestBody TrendDO trendDO) {
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            return ResultDTOBuilder.failure("0000005", "必传参数为空", null);
        }
        // 获取登录用户
        LoginUser loginUser = super.getLoginUser(token);
        if (loginUser == null ) { // 判断是否登录
            return ResultDTOBuilder.failure("0000001", "登录已过期", null);
        }
        trendDO.setUserId(loginUser.getId());
        return trendService.addTrend(trendDO);
    }

    // 删除动态
    @PostMapping("/deleteTrend")
    public ResultDTO deleteTrend (HttpServletRequest request, @RequestBody TrendDO trendDO) {
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            return ResultDTOBuilder.failure("0000005", "必传参数为空", null);
        }
        // 获取登录用户
        LoginUser loginUser = super.getLoginUser(token);
        if (loginUser == null ) { // 判断是否登录
            return ResultDTOBuilder.failure("0000001", "登录已过期", null);
        }
        trendDO.setUserId(loginUser.getId());
        return trendService.deleteTrend(trendDO);
    }

    // 动态点赞收藏
    @PostMapping("/addTrendHeart")
    public ResultDTO addTrendHeart (HttpServletRequest request, @RequestBody TrendHeartDO trendHeartDO) {
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            return ResultDTOBuilder.failure("0000005", "必传参数为空", null);
        }
        // 获取登录用户
        LoginUser loginUser = super.getLoginUser(token);
        if (loginUser == null ) { // 判断是否登录
            return ResultDTOBuilder.failure("0000001", "登录已过期", null);
        }
        trendHeartDO.setUserId(loginUser.getId());
        return trendHeartService.addTrendHeart(trendHeartDO);
    }

    // 取消动态点赞收藏
    @PostMapping("/cancleTrendHeart")
    public ResultDTO cancleTrendHeart (HttpServletRequest request, @RequestBody TrendHeartDO trendHeartDO) {
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            return ResultDTOBuilder.failure("0000005", "必传参数为空", null);
        }
        // 获取登录用户
        LoginUser loginUser = super.getLoginUser(token);
        if (loginUser == null ) { // 判断是否登录
            return ResultDTOBuilder.failure("0000001", "登录已过期", null);
        }
        trendHeartDO.setUserId(loginUser.getId());
        return trendHeartService.cancleTrendHeart(trendHeartDO);
    }

    // 查询我的动态列表
    @PostMapping("/queryMyTrendList")
    public ResultDTO queryMyTrendList (HttpServletRequest request, @RequestBody QueryParamBO queryParamBO) {
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            return ResultDTOBuilder.failure("0000005", "必传参数为空", null);
        }
        // 获取登录用户
        LoginUser loginUser = super.getLoginUser(token);
        if (loginUser == null ) { // 判断是否登录
            return ResultDTOBuilder.failure("0000001", "登录已过期", null);
        }
        Integer userId = queryParamBO.getUserId() == null ? loginUser.getId() : queryParamBO.getUserId();
        return trendService.queryMyTrendList(userId, loginUser.getId());
    }

    // 查询我的点赞收藏列表
    @PostMapping("/queryMyTrendHeartList")
    public ResultDTO queryMyTrendHeartList (HttpServletRequest request, @RequestBody QueryParamBO queryParamBO) {
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            return ResultDTOBuilder.failure("0000005", "必传参数为空", null);
        }
        // 获取登录用户
        LoginUser loginUser = super.getLoginUser(token);
        if (loginUser == null ) { // 判断是否登录
            return ResultDTOBuilder.failure("0000001", "登录已过期", null);
        }
        queryParamBO.setUserId(loginUser.getId());
        return trendService.queryMyTrendHeartList(queryParamBO, getPageQuery(queryParamBO.getPageNo(), queryParamBO.getPageSize()));
    }

    // 查询圈子动态数据
    @PostMapping("/queryCircleTrendList")
    public ResultDTO queryCircleTrendList (HttpServletRequest request, @RequestBody QueryParamBO queryParamBO) {
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            return ResultDTOBuilder.failure("0000005", "必传参数为空", null);
        }
        // 获取登录用户
        LoginUser loginUser = super.getLoginUser(token);
        if (loginUser == null ) { // 判断是否登录
            return ResultDTOBuilder.failure("0000001", "登录已过期", null);
        }
        queryParamBO.setUserId(loginUser.getId());
        return trendService.queryTrendList(queryParamBO, getPageQuery(queryParamBO.getPageNo(), queryParamBO.getPageSize()));
    }

    // 上传动态评论图片
    @PostMapping("/uploadTrendCommentUrl")
    public ResultDTO uploadTrendCommentUrl (HttpServletRequest request, MultipartFile[] lstFile) {
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            return ResultDTOBuilder.failure("0000005", "必传参数为空", null);
        }
        // 获取登录用户
        LoginUser loginUser = super.getLoginUser(token);
        if (loginUser == null ) { // 判断是否登录
            return ResultDTOBuilder.failure("0000001", "登录已过期", null);
        }
        return activityService.uploadFile(lstFile, loginUser.getId(), "trendCommentUrl");
    }

    // 添加动态评论
    @PostMapping("/addTrendComment")
    public ResultDTO addTrendComment (HttpServletRequest request, @RequestBody TrendCommentDO trendCommentDO) {
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            return ResultDTOBuilder.failure("0000005", "必传参数为空", null);
        }
        // 获取登录用户
        LoginUser loginUser = super.getLoginUser(token);
        if (loginUser == null ) { // 判断是否登录
            return ResultDTOBuilder.failure("0000001", "登录已过期", null);
        }
        trendCommentDO.setCommentUserId(loginUser.getId());
        return trendService.addTrendComment(trendCommentDO);
    }

    // 删除动态评论
    @PostMapping("/deleteTrendComment")
    public ResultDTO deleteTrendComment (HttpServletRequest request, @RequestBody TrendCommentDO trendCommentDO) {
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            return ResultDTOBuilder.failure("0000005", "必传参数为空", null);
        }
        // 获取登录用户
        LoginUser loginUser = super.getLoginUser(token);
        if (loginUser == null ) { // 判断是否登录
            return ResultDTOBuilder.failure("0000001", "登录已过期", null);
        }
        trendCommentDO.setCommentUserId(loginUser.getId());
        return trendService.deleteTrendComment(trendCommentDO);
    }

    // 点赞动态评论
    @PostMapping("/addTrendCommentHeart")
    public ResultDTO addTrendCommentHeart (HttpServletRequest request, @RequestBody TrendCommentHeartDO trendCommentHeartDO) {
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            return ResultDTOBuilder.failure("0000005", "必传参数为空", null);
        }
        // 获取登录用户
        LoginUser loginUser = super.getLoginUser(token);
        if (loginUser == null ) { // 判断是否登录
            return ResultDTOBuilder.failure("0000001", "登录已过期", null);
        }
        trendCommentHeartDO.setUserId(loginUser.getId());
        return trendService.addTrendCommentHeart(trendCommentHeartDO);
    }

    // 取消动态评论点赞
    @PostMapping("/cancleTrendCommentHeart")
    public ResultDTO cancleTrendCommentHeart (HttpServletRequest request, @RequestBody TrendCommentHeartDO trendCommentHeartDO) {
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            return ResultDTOBuilder.failure("0000005", "必传参数为空", null);
        }
        // 获取登录用户
        LoginUser loginUser = super.getLoginUser(token);
        if (loginUser == null ) { // 判断是否登录
            return ResultDTOBuilder.failure("0000001", "登录已过期", null);
        }
        trendCommentHeartDO.setUserId(loginUser.getId());
        return trendService.cancleTrendCommentHeart(trendCommentHeartDO);
    }

    // 查询动态评论列表
    @PostMapping("/queryTrendCommentList")
    public ResultDTO queryTrendCommentList (HttpServletRequest request, @RequestBody QueryParamBO queryParamBO) {
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            return ResultDTOBuilder.failure("0000005", "必传参数为空", null);
        }
        // 获取登录用户
        LoginUser loginUser = super.getLoginUser(token);
        if (loginUser == null ) { // 判断是否登录
            return ResultDTOBuilder.failure("0000001", "登录已过期", null);
        }
        queryParamBO.setUserId(loginUser.getId());
        return trendService.queryTrendCommentList(queryParamBO);
    }

    // 查询动态列表
    @PostMapping("/queryTrendList")
    public ResultDTO queryTrendList (HttpServletRequest request, @RequestBody QueryParamBO queryParamBO) {
        String token = request.getHeader("token");
        if(!StringUtils.isEmpty(token)){
            // 获取登录用户
            LoginUser loginUser = super.getLoginUser(token);
            if (loginUser == null ) { // 判断是否登录
                return ResultDTOBuilder.failure("0000001", "登录已过期", null);
            }
            queryParamBO.setUserId(loginUser.getId());
        }
        return trendService.queryTrendList(queryParamBO, getPageQuery(queryParamBO.getPageNo(), queryParamBO.getPageSize()));
    }

    // 查询动态详情
    @PostMapping("/queryTrendDetail")
    public ResultDTO queryTrendDetail (HttpServletRequest request, @RequestBody TrendDO trendDO) {
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            return ResultDTOBuilder.failure("0000005", "必传参数为空", null);
        }
        // 获取登录用户
        LoginUser loginUser = super.getLoginUser(token);
        if (loginUser == null ) { // 判断是否登录
            return ResultDTOBuilder.failure("0000001", "登录已过期", null);
        }
        trendDO.setUserId(loginUser.getId());
        return trendService.queryTrendDetail(trendDO.getTrendId(), loginUser.getId());
    }
}
